<?php
/**
 * Created by PhpStorm.
 * User: jiaosuxin(Silas@sf-express.com)
 * Date: 2019-07-15
 * Time: 16:01
 */

include '../PHPExcel/Classes/PHPExcel.php';
include '../library/PHPExcelHelper.php';
include '../library/ExplainHelper.php';

function mockData() {
    $return = [];

    for ($i = 0; $i < 1000; $i++) {
        $return[] = [
            'id' => uniqid(),
            'order_id' => '00' . uniqid(),
            'date' => date('Y-m-d H:i:s', time() - rand(10, 6280000)),
            'city_code' => 'Q010Y',
            'customer_name' => '客户名称' . uniqid() . str_pad('我', 2000, '知'),
            'money' => rand(1, 100000) - rand(1, 100000) + 0.01,
        ];
    }

    return $return;
}

ExplainHelper::start();
ini_set("memory_limit", "4G");

$headers = [
    ['key' => 'id', 'label' => '序号'],
    ['key' => 'order_id', 'label' => '订单编号'],
    ['key' => 'date', 'label' => '日期'],
    ['key' => 'city_code', 'label' => '城市代码'],
    ['key' => 'customer_name', 'label' => '客户名称'],
    ['key' => 'money', 'label' => '总金额'],
];
$data = mockData();

$filename = '../../tmp/' . date('Ymd-H-i-s') . '.xlsx';

$excel = new PHPExcel();
$sheet = $excel->getSheet(0);

// 设置header
$line = 1;
$idx = 0;
foreach ($headers as $header) {
    $sheet->setCellValueExplicitByColumnAndRow($idx++, $line, $header['label'], \PHPExcel_Cell_DataType::TYPE_STRING);
}
$line++;


// 设置data
foreach ($data as $row) {
    $idx = 0;
    foreach ($headers as $header) {
        $sheet->setCellValueExplicitByColumnAndRow($idx++, $line, $row[$header['key']], \PHPExcel_Cell_DataType::TYPE_STRING);
    }
    $line++;
}



$writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');

$writer->save($filename);


ExplainHelper::end();